/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemmanager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.poemmanager.domain.Comment;
import me.zhengjie.poemmanager.domain.vo.CommentQueryCriteria;
import me.zhengjie.poemmanager.mapper.CommentMapper;
import me.zhengjie.poemmanager.service.CommentService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author hwl
* @date 2024-12-06
**/
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final CommentMapper commentMapper;

    @Override
    public PageResult<Comment> queryAll(CommentQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(commentMapper.findAll(criteria, page));
    }

    @Override
    public List<Comment> queryAll(CommentQueryCriteria criteria){
        return commentMapper.findAll(criteria);
    }

    /**（0 post,1 poem_user,2 poem_poet,3 recitation）
     *
     * @param resources /
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Comment resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Comment resources) {
        Comment comment = getById(resources.getCommentId());
        comment.copy(resources);
        saveOrUpdate(comment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Comment> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Comment comment : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("评论的帖子/诗歌/朗诵的主键id", comment.getObjectId());
            map.put("评论的地方的类别（0帖子，1诗歌，2朗诵）", comment.getObjectType());
            map.put("用户名", comment.getUserName());
            map.put("评论内容", comment.getContent());
            map.put("创建日期", comment.getCreatedAt());
            map.put("更新时间", comment.getUpdatedAt());
            map.put("回复的评论的id", comment.getParentId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Comment> getMyComment(String userName) {
        List<Comment>cs = commentMapper.getCommentByUserId(userName);
        for (Comment c:cs
             ) {
            switch (c.getObjectType()){
                case 0:
                    c.setTitle(commentMapper.getPostTitle(c.getObjectId()));
                    break;
                case 1:c.setTitle(commentMapper.getPoemUserTitle(c.getObjectId()));
                    break;
                case 2:c.setTitle(commentMapper.getPoemPoetTitle(c.getObjectId()));
                    break;
                case 3:c.setTitle(commentMapper.getRecitationTitle(c.getObjectId()));
                    break;
            }
        }
        return  cs ;
    }

}